//
//  FirstDetailViewController.m
//  Protect -A
//
//  Created by 付剑英 on 16/6/21.
//  Copyright © 2016年 付剑英. All rights reserved.
//

#import "FirstDetailViewController.h"
#import "baseTableview.h"
#import "FirDetailTableViewCell.h"
#import "APPTools.h"
#import "ModelOfFirst.h"
#import "FirstDetailDetailViewController.h"
#import <DKNightVersion.h>
#import <MJRefresh.h>
@interface FirstDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain)baseTableview *tableview1;
@property (nonatomic,retain)NSMutableArray *arrData;//数据
@property (nonatomic,assign)NSInteger page;//用来保存请求新数据的动态值


@end

@implementation FirstDetailViewController


#pragma mark - ------------ 内存管理 ---------------
-(void)dealloc{
    [_tableview1 release];
    [_arrData release];
    [_NaviTitle release];
    [_strID release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handeData];
    [self createTableview];
    [self refresh];
    self.navigationItem.title = _NaviTitle;
    // Do any additional setup after loading the view.
}



#pragma mark - ------------ 数据处理 ---------------
-(void)handeData{

    NSString *newContent = [NSString stringWithFormat:@"https://open.seriousapps.cn/3/enjoy_product/group_product_list.json?city_id=1&count=20&group_id=%@&lat=38.88336921532342&lng=121.5446045366585&page=0&sort=1", _strID ];
     NSString *new = [newContent stringByReplacingOccurrencesOfString:@"sort=1" withString:[NSString stringWithFormat:@"sort=%ld", _page]];
    [APPTools GETWithURL:new  par:nil success:^(id responseObject) {
        
        if (_page == 1) {
            
            [_arrData removeAllObjects];
        }
        self.arrData = [NSMutableArray array];
        NSArray *arr = responseObject[@"content"];
        NSArray *arrModel  = [ModelOfFirst  modelHandleWithArrar:arr];
        
        [_arrData addObjectsFromArray:arrModel];
        
        [self.tableview1 reloadData];
    } filed:^(NSError *error) {
        NSLog(@"数据加载失败");
    }];
}

#pragma mark - ------------ 刷新加载 ---------------

- (void)refresh
{
    //刷新
    self.tableview1.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self handeData];
        [_tableview1.mj_header endRefreshing];
    }];
    
    //加载
    self.tableview1.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        _page += 1;
        [self handeData];
        [_tableview1.mj_footer endRefreshing];
    }];
    
    
}




#pragma mark - ------------ 创建tableview ---------------
-(void)createTableview{
    
    self.tableview1 =[[baseTableview alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableview1];
   
    _tableview1.delegate = self;
    _tableview1.dataSource = self;
    [_tableview1 registerClass:[FirDetailTableViewCell class] forCellReuseIdentifier:@"pool1"];
    [_tableview1 release];
}
#pragma mark - ------------ 返回分区数 ---------------


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _arrData.count;
}

#pragma mark - ------------ cell赋值 ---------------

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FirDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    ModelOfFirst *model1 = _arrData[indexPath.row];
    cell.model = model1;
    return cell;

}
#pragma mark - ------------ 返回高度 ---------------

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 180;
    
}
#pragma mark - ------------ 跳转第3页 ---------------

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  
    FirstDetailDetailViewController *vc3 = [[FirstDetailDetailViewController alloc]init];
     ModelOfFirst *model = _arrData[indexPath.row];
     vc3.DetailID = model.product_id;
    [self.navigationController pushViewController:vc3 animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
